package com.zwk.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.zwk.gulimall.product.annotation.RelationMethod;
import com.zwk.gulimall.product.entity.BrandEntity;
import com.zwk.gulimall.product.entity.CategoryEntity;
import com.zwk.gulimall.product.service.BrandService;
import com.zwk.gulimall.product.service.CategoryService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwk.common.utils.PageUtils;
import com.zwk.common.utils.Query;

import com.zwk.gulimall.product.dao.CategoryBrandRelationDao;
import com.zwk.gulimall.product.entity.CategoryBrandRelationEntity;
import com.zwk.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brandEntity = brandService.getById(brandId);
        CategoryEntity categoryEntity = categoryService.getById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateDetail(BrandEntity brandEntity) {
        if (brandEntity == null || Strings.isEmpty(brandEntity.getName())) {
            return;
        }
        this.update(new LambdaUpdateWrapper<CategoryBrandRelationEntity>()
                .set(CategoryBrandRelationEntity::getBrandName, brandEntity.getName())
                .eq(CategoryBrandRelationEntity::getBrandId,brandEntity.getBrandId())
        );
    }

    @Override
    public void updateDetail(CategoryEntity categoryEntity) {
        if (categoryEntity == null || Strings.isEmpty(categoryEntity.getName())) {
            return;
        }
        this.update(new LambdaUpdateWrapper<CategoryBrandRelationEntity>()
                .set(CategoryBrandRelationEntity::getCatelogName, categoryEntity.getName())
                .eq(CategoryBrandRelationEntity::getCatelogId,categoryEntity.getCatId())
        );
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> list = this.list(
                new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getCatelogId, catId)
        );
        List<Long> ids = list.stream().map(relation -> relation.getBrandId()).collect(Collectors.toList());
        return brandService.listByIds(ids);
    }

}